# 定义一个用户类,用户名和密码是这个类的属性,实例化两个用户,分别有不同的用户名和密码
        # 登陆成功之后才创建用户对象
        # 设计一个方法 修改密码
import os
def login(name,passwd,filepath='userinfo'):
    with open(filepath,encoding='utf-8') as f:
        for line in f:
            user,pwd=line.strip().split('|')
            if user==name and passwd==pwd:
                return True,name,pwd
            else:
                return False
class User:
    def __init__(self,username,password):
        self.user=username
        self.pwd=password

    def change_pwd(self):
        old_pwd=input('输入原密码:')
        new_pwd = input('输入新密码:')
        flag=False
        with open('userinfo',encoding='utf-8') as f1,open('userinfo.bak',encoding='utf-8',mode='w') as f2:
            for line in f1:
                username,password=line.strip().split('|')
                if username==self.user and password==old_pwd:
                    line='%s|%s'%(username,new_pwd)
                    flag=True
                f2.write(line)
                f2.write('\n')

        os.remove('userinfo')
        os.rename('userinfo.bak','userinfo')
        return flag
name=input('user:')
passwd=input('pwd:')
ret=login(name,passwd)
if ret:
    print('登陆成功')
    obj=User(name,passwd)
    res=obj.change_pwd()
    if res:
        print('修改成功')
else:
    print('登陆失败')
    